package com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.entity.KnowledgeManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.service.KnowledgeManagementService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.vo.KnowledgeManagementVO;

import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/11
 * @Description: 知识库管理
 */
@RestController
@RequestMapping("/knowledgeManagement")
@Slf4j
@Api(tags = "知识库管理")
public class KnowledgeManagementController {

    @Resource
    private KnowledgeManagementService knowledgeManagementService;

    /**
     * 新增单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("save")
    @Operation(summary = "新增数据")
    public RestResponse save(@RequestBody @Valid KnowledgeManagement param) {
        try {
            knowledgeManagementService.save(param);
            return RestResponse.succeed("新增成功");
        } catch (Exception e) {
            log.error("新增数据失败!", e);
            return RestResponse.fail("新增数据失败!");
        }
    }

    /**
     * 修改单条数据
     *
     * @param param
     * @return
     */
    @PostMapping("updateById")
    @Operation(summary = "修改单条数据")
    public RestResponse updateById(@RequestBody @Valid KnowledgeManagement param) {
        try {
            knowledgeManagementService.updateById(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改数据失败!", e);
            return RestResponse.fail("修改数据失败!");
        }
    }

    /**
     * 删除单条数据
     *
     * @param Id
     * @return
     */
    @PostMapping("deleteById/{id}")
    @Operation(summary = "删除单条数据")
    public RestResponse deleteById(@PathVariable("id")String Id) {
        if (StringUtils.isBlank(Id)) {
            return RestResponse.fail("Id不能为空!");
        }
        try {
            knowledgeManagementService.deleteById(Id);
            return RestResponse.succeed("删除成功");
        } catch (Exception e) {
            log.error("删除数据失败!", e);
            return RestResponse.fail("删除数据失败!");
        }
    }

    /**
     * 分页列表
     *
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(KnowledgeManagement queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = knowledgeManagementService.getPage(queParams, curPage, pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 附件上传
     *
     * @param Id
     * @return
     */
    @GetMapping("updateFile")
    @Operation(summary = "附件上传")
    public RestResponse updateFile(String Id, String fileId, String FileType,String attachmentName) {
        if (StringUtils.isBlank(Id)) {
            return RestResponse.fail("Id不能为空!");
        }
        if (StringUtils.isBlank(fileId)) {
            return RestResponse.fail("文件id不能为空!");
        }
        if (StringUtils.isBlank(FileType)) {
            return RestResponse.fail("文件类型不能为空!");
        }
        try {
            knowledgeManagementService.updateFile(Id, fileId, FileType,attachmentName);
            return RestResponse.succeed("附件上传成功");
        } catch (Exception e) {
            log.error("附件上传失败!", e);
            return RestResponse.fail("附件上传失败!");
        }
    }

    /**
     * 解析excel
     *
     * @param fileId
     * @param xsemId
     * @return
     */
    @GetMapping("parseExcel")
    @Operation(summary = "解析excel")
    public RestResponse parseExcel(String fileId, String xsemId) {
        if (StringUtils.isBlank(fileId)) {
            return RestResponse.fail("Id不能为空!");
        }
        try {
            knowledgeManagementService.parseExcel(fileId, xsemId);
            return RestResponse.succeed("解析成功");
        } catch (Exception e) {
            log.error("解析数据失败!", e);
            return RestResponse.fail("解析数据失败!");
        }
    }

    /**
     * 解析excel
     * @param file
     * @param xsemId
     * @return
     */
    @PostMapping("importExcel")
    public RestResponse parseExcel(@RequestParam("file") MultipartFile file, String xsemId) {
        RestResponse restResponse=null;
        try {
            return knowledgeManagementService.importExcel(file,xsemId);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "导入失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     *
     *
     * @return
     */
    @GetMapping("getKnowledgeCode")
    public RestResponse getKnowledgeCode(String xsemId) {
        try {
            KnowledgeManagement code = knowledgeManagementService.getKnowledgeCode(xsemId);
            return RestResponse.succeed(code);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    @Operation(summary = "导出数据")
    @PostMapping("exportData")
    public void exportData(HttpServletResponse response,@RequestBody KnowledgeManagement param) {
        knowledgeManagementService.exportData(response, param);
    }


    @GetMapping("findById")
    @Operation(summary = "根据id查询")
    public RestResponse findById(String id) {
        try {
            KnowledgeManagementVO data = knowledgeManagementService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 获取编号
     * @param eventCode
     * @return
     */
    @GetMapping("/getCode")
    public RestResponse getCode(String eventCode){
        RestResponse response = null;

        try{
            String str=knowledgeManagementService.getCode(eventCode);
            response = RestResponse.succeed(str);
        }catch (Exception e){
            String errorMessage = "生成失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
